package com.example.cofeed.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.blankj.utilcode.util.ObjectUtils;
import com.example.cofeed.R;
import com.example.cofeed.adapter.AttentionLeftAdapter;
import com.example.cofeed.adapter.AttentionRightAdapter;
import com.example.cofeed.api.ApiService;
import com.example.cofeed.base.BaseActivity;
import com.example.cofeed.callback.DialogCallback;
import com.example.cofeed.callback.LzyResponse;
import com.example.cofeed.entity.Attention;
import com.example.cofeed.entity.Navigator;
import com.example.cofeed.even.RefreshEven;
import com.lzy.okgo.model.Response;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AttentionMoreActivity extends BaseActivity {

    @BindView(R.id.rv_left)
    RecyclerView mRvLeft;
    @BindView(R.id.rv_right)
    RecyclerView mRvRight;
    private AttentionLeftAdapter mAttentionLeftAdapter;
    private AttentionRightAdapter mAttentionRightAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_attention_more;
    }

    @Override
    protected void init(Bundle savedInstanceState, View container) {
        setMainTitle("关注更多");

        mRvLeft.setLayoutManager(new LinearLayoutManager(this));
        mAttentionLeftAdapter = new AttentionLeftAdapter();
        mRvLeft.setAdapter(mAttentionLeftAdapter);

        mAttentionLeftAdapter.setOnItemClickListener((adapter, view, position) -> {
            Attention attention = (Attention) adapter.getData().get(position);
            mAttentionLeftAdapter.setSelect(attention);
            getSubData(attention.catid);


        });

        mRvRight.setLayoutManager(new LinearLayoutManager(this));
        mAttentionRightAdapter = new AttentionRightAdapter();
        mRvRight.setAdapter(mAttentionRightAdapter);

        mAttentionRightAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            Attention attention = (Attention) adapter.getData().get(position);
            if (ObjectUtils.isNotEmpty(attention.third_menu)){
//                if (attention.isExpend){
//                    ((ImageView) view).setImageResource(R.mipmap.ic_down);
//                }else {
//                    ((ImageView) view).setImageResource(R.mipmap.ic_up);
//                }
                mAttentionRightAdapter.setIsExpend(attention);
                return;

            }


            if (attention.ispush == 0){
                attention.ispush = 1;
            }else {
                attention.ispush = 0;
            }
//            ApiService.followinfo_on_off(this, attention.ispush , attention.catid, attention.chid, new DialogCallback<LzyResponse<Object>>(AttentionMoreActivity.this) {
            ApiService.followinfo_add(this, attention.catid, attention.chid, attention.areaid, new DialogCallback<LzyResponse<Object>>(AttentionMoreActivity.this) {
                @Override
                public void onSuccess(Response<LzyResponse<Object>> response) {
//                    if (attention.ispush == 0){
//                        attention.ispush = 1;
//                    }else {
//                        attention.ispush = 0;
//                    }
                    mAttentionRightAdapter.notifyDataSetChanged();
//                    mAttentionRightAdapter.setSelect(attention);

                }
            });

        });

        ApiService.followinfo_followlist(this, new DialogCallback<LzyResponse<List<Attention>>>(this) {
            @Override
            public void onSuccess(Response<LzyResponse<List<Attention>>> response) {
                mAttentionLeftAdapter.setNewData(response.body().data);
                if (ObjectUtils.isEmpty(response.body().data)){
                    return;
                }
                mAttentionRightAdapter.setSelect(response.body().data.get(0));
                getSubData(response.body().data.get(0).catid);
            }
        });

    }

    private void getSubData(int id){
        ApiService.followinfo_subfollowlist(this, id, new DialogCallback<LzyResponse<List<Attention>>>(this) {
            @Override
            public void onSuccess(Response<LzyResponse<List<Attention>>> response) {
                mAttentionRightAdapter.setNewData(response.body().data);
                if (ObjectUtils.isEmpty(response.body().data)){
                    View inflate = LayoutInflater.from(mContext).inflate(R.layout.include_no_data, null);
                    inflate.setVisibility(View.VISIBLE);
                    mAttentionRightAdapter.setEmptyView(inflate);
                }
            }
        });
    }

    @Override
    public void finish() {
        super.finish();
        EventBus.getDefault().post(new RefreshEven(0));
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
